#include "StdAfx.h"
#include "ImpostorDemoState.h"

#include "CoreEngine.h"
#include "MainState.h"

#include "Entity.h"
#include "PrimitiveLineList.h"
#include "AABB.h"


ImpostorDemoState::ImpostorDemoState()
	: FEApplication(), FEngine::KeyboardInputListener(), FEngine::MouseInputListener()
{
	mCameraHandler			= NULL;
	mStatPanel				= NULL;
	mForestLoader			= NULL;
	mGround					= NULL;
}

ImpostorDemoState::~ImpostorDemoState()
{
	FEngine::InputManager::getInstance()->unregisterKeyboardInputListener(this);
	FEngine::InputManager::getInstance()->unregisterMouseInputListener(this);

	delete mCameraHandler;
	delete mStatPanel;
	if (mForestLoader)
		delete mForestLoader;
	
	for (std::list<FEngine::TextLabel*>::iterator it = mListLabels.begin(); it != mListLabels.end(); ++it)
		delete *it;

	if (mGround)
		delete mGround;
}

void ImpostorDemoState::initialize()
{
	FEngine::FEApplication::initialize();
	FEngine::InputManager::getInstance()->registerKeyboardInputListener(this);
	FEngine::InputManager::getInstance()->registerMouseInputListener(this);
	
	FEngine::CoreEngine::getInstance()->getCamera()->setFarDistanceView(750.0f);
	
	mCameraHandler = new FEngine::FreeCameraHandler(FEngine::CoreEngine::getInstance()->getCamera(), Vector3(0, 3, -50), Vector3(0, 0, 1));
	addChild(mCameraHandler);
	
	FEngine::CoreEngine::getInstance()->disableFog();
	
	FEngine::CoreEngine::getInstance()->setSkyDome("skydome1.dds", 1.0f);

	mStatPanel = new StatPanel();
	addGUIElement(mStatPanel);

	
	mListLabels.push_back(new FEngine::TextLabel("This show how the impostors work", 350, 10, 0, 200, 22, FEngine::ColorRGB(255, 255, 255)));
	addGUIElement(mListLabels.back());
	mListLabels.push_back(new FEngine::TextLabel("Impostors are billboards which replace far 3D objects", 350, 40, 0, 200, 22, FEngine::ColorRGB(255, 255, 255)));
	addGUIElement(mListLabels.back());
	mListLabels.push_back(new FEngine::TextLabel("Move around the object to see how the impostor works", 350, 70, 0, 200, 22, FEngine::ColorRGB(255, 255, 255)));
	addGUIElement(mListLabels.back());

	// Create one object with a low impostor distance
	mForestLoader = new FEngine::ForestLoader(20, 20, Vector2(-10, -10), 750.0f, 40.0f);
	mForestLoader->createObject("chair.x", "ChairDiff.dds");
//	mForestLoader->createObject("aster1.x", "aster1.dds");
	mForestLoader->addObject(Vector3(0, 2, 0), Vector3(0, 0, (float)(rand() % 360)), Vector3(1.0f, 1.0f, 1.0f));
	
	// Create the ground
	mGround = new FEngine::PrimitiveCube(1000.0f);
	addChild(mGround);
	mGround->translate(0, -1000.0f, 0);
	mGround->setColor(20, 50, 20);

}

void ImpostorDemoState::update(double _elapsedTime)
{
	FEApplication::update(_elapsedTime);

	mForestLoader->update(_elapsedTime);
}

void ImpostorDemoState::updateGUI(double _elapsedTime)
{
	FEApplication::updateGUI(_elapsedTime);
}

void ImpostorDemoState::render(double _elapsedTime)
{
	FEApplication::render(_elapsedTime);

	mForestLoader->render(_elapsedTime);
}

void ImpostorDemoState::keyPressed(int _key)
{
	if (_key == FEngine::InputManager::KEY_ESCAPE)
	{
		FEngine::CoreEngine::getInstance()->setApplication(new MainState());
	}
}

void ImpostorDemoState::keyReleased(int _key)
{
}

void ImpostorDemoState::mouseButtonPressed(int _button, const Vector2& _position)
{
}

void ImpostorDemoState::mouseButtonReleased(int _button, const Vector2& _position)
{
}

void ImpostorDemoState::mouseMoved(const Vector2& _delta)
{
}
